package com.hxzy.projectssmplus.controller;

import com.hxzy.projectssmplus.entity.SysArea;
import com.hxzy.projectssmplus.service.SysAreaService;
import com.hxzy.projectssmplus.vo.AjaxResult;
import com.hxzy.projectssmplus.vo.AreaTreeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: zdk
 * Date: 2021/12/07 11:20
 * Description:
 * Version: V1.0
 */
@RestController
@RequestMapping(value = "/SysArea")
public class SysAreaController {
    @Autowired
    private SysAreaService sysAreaService;

    /**
     * 地区列表加载
     * @param parentId
     * @return
     */
    @GetMapping(value = "/RootArea")
    public AjaxResult getAreaList(Long parentId){
        List<SysArea> areaTree = this.sysAreaService.getAreaList(parentId);
        return AjaxResult.success_data(areaTree);
    }

    @PostMapping(value="/add")
    public AjaxResult insert(SysArea sysArea){
        boolean save = this.sysAreaService.save(sysArea);
        return save?AjaxResult.success("添加成功"):AjaxResult.error("添加失败");
    }

    @PostMapping(value = "/update")
    public AjaxResult update(SysArea sysArea){
        boolean updateById = this.sysAreaService.updateById(sysArea);
        return updateById?AjaxResult.success("修改成功"):AjaxResult.error("修改失败");
    }

    /**
     * 地区树加载(三级列表)
     */
    @GetMapping(value = "/areaTree")
    public AjaxResult getAreaTree(){
        List<AreaTreeVO> areaTree = this.sysAreaService.getAreaTree();
        return AjaxResult.success_data(areaTree);
    }

}
